<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>035-Acticity详解-基础&进阶</title>
  
  
  <meta property="og:title" content="035-Acticity详解-基础&amp;进阶">
  
  
  <meta property="og:url" content="http://example.com/2020/11/03/035-Acticity%E8%AF%A6%E8%A7%A3-%E5%9F%BA%E7%A1%80&amp;%E8%BF%9B%E9%98%B6/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2020-11-03">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Activity">
  
  <meta property="og:article:tag" content="Intent">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      035-Acticity详解-基础&进阶
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2020-11-03T04:09:31.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2020-11-03</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>1.4k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Activity/" class="post-meta-link">Activity</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Intent/" class="post-meta-link">Intent</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/article/details/115579861">【5年Android从零复盘系列之三十五】Acticity进阶详解</a></p>
<hr/>

<p>【代码是最好得老师】</p>
<h1 id="1-概要"><a href="#1-概要" class="headerlink" title="1.概要"></a>1.概要</h1><p>Activity 是四大组件之首</p>
<h1 id="2-Activity生命周期"><a href="#2-Activity生命周期" class="headerlink" title="2.Activity生命周期"></a>2.Activity生命周期</h1><ul>
<li>典型情况下的生命周期：</li>
</ul>
<p>是指在有用户参与的情况下，Activity所经过的生命周期的改变；</p>
<ul>
<li>异常情况下的生命周期：</li>
</ul>
<p>是指Activity <strong>被系统回收</strong> 或者由于当前设备的 <strong>Configuration发生改变</strong> 从而导致Activity被销毁重建</p>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210410105423_4D9b0d.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210410105423_4D9b0d.png"/>

<h2 id="2-1典型生命周期"><a href="#2-1典型生命周期" class="headerlink" title="2.1典型生命周期"></a>2.1典型生命周期</h2><ul>
<li>当用户按back键回退时，回调如下：onPause -&gt; onStop -&gt; onDestroy</li>
</ul>
<h2 id="2-2异常生命周期"><a href="#2-2异常生命周期" class="headerlink" title="2.2异常生命周期"></a>2.2异常生命周期</h2><ul>
<li>如果突然旋转屏幕，由于系统配置发生了改变，在默认情况下，Activity就会被销毁并且重新创建</li>
<li>Activity是在异常情况下终止的，系统会调用onSaveInstanceState来保存当前Activity的状态</li>
<li>Activity被意外终止时，Activity会调用onSaveInstanceState去保存数据，然后Activity会委托Window去保存数据，接着Window再委托它上面的顶级容器去保存数据。顶层容器是一个ViewGroup，一般来说它很可能是DecorView。最后顶层容器再去一一通知它的子元素来保存数据，这样整个数据保存过程就完成了</li>
<li>onRestoreInstanceState一旦被调用，其参数Bundle savedInstanceState一定是有值的，我们不用额外地判断是否为空；但是onCreate不行，onCreate如果是正常启动的话，其参数Bundle savedInstanceState为null，所以必须要额外判断</li>
</ul>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210410141729_9e5OwM.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210410141729_9e5OwM.png"/>

<h2 id="2-3-configChanges"><a href="#2-3-configChanges" class="headerlink" title="2.3 configChanges"></a>2.3 configChanges</h2><p>不想让Activity在屏幕旋转的时候重新创建，就可以给configChanges属性添加orientation这个值</p>
<pre class="highlight"><span class="line">android:configChanges=&quot;orientation&quot;</span><br><span class="line">android:configChanges=&quot;orientation|screenSize&quot;</span><br><span class="line">android:configChanges=&quot;orientation|keyboardHidden&quot;</span><br></pre>
<p>Activity的确没有重新创建，并且也没有调用onSaveInstanceState和onRestoreInstanceState来存储和恢复数据，取而代之的是系统调用了Activity的<strong>onConfigurationChanged</strong>方法</p>
<img src="https:/ srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/20210410143716_Esmq5X.png" class="lozad post-image"/gitee.com/cupster/picbed/raw/master/20210410143716_Esmq5X.png"/>


<p><strong>上面表格中的项目很多，但是我们常用的只有locale、orientation和keyboardHidden这三个选项</strong></p>
<h1 id="3-Activity的启动过程"><a href="#3-Activity的启动过程" class="headerlink" title="3.Activity的启动过程"></a>3.Activity的启动过程</h1><ol>
<li>启动Activity的请求会交由Instrumentation处理</li>
<li>Instrumentation通过Binder向AMS发送请求</li>
<li>AMS(Activity Manager Service)内部维护着一个ActivityStack，并负责同步栈内Activity的状态</li>
<li>AMS通过ActivityThread去同步Activity状态，完成生命周期方法调用</li>
</ol>
<h1 id="4-Activity-启动模式"><a href="#4-Activity-启动模式" class="headerlink" title="4.Activity 启动模式"></a>4.Activity 启动模式</h1><ul>
<li>addFlags的优先级要高于xml方式</li>
<li>clearTaskOnLaunch设置该属性后，每次返回该Activity时，都将该Activity之上的所有Activity清除</li>
<li>TaskAffinity：标识了一个Activity所需要的任务栈的名字，默认情况下，所有Activity所需的任务栈的名字为应用的包名</li>
<li>singleTop &amp;  singleInstance 这两种启动模式，，使用startActivityForResult()方法启动另一个Activity ，那么系统更直接返回  Activity.RESULT_CANCELED，不会等待返回，因为android在Framework层做了对这两启动模式的限制<br><br/><br/>根本： 不同Task栈，不能进行数据数据，如果一定要，只能通过Intent来绑定数据 </li>
</ul>
<h2 id="4-1标准模式-standard"><a href="#4-1标准模式-standard" class="headerlink" title="4.1标准模式-standard"></a>4.1标准模式-standard</h2><ol>
<li>系统默认的启动模式</li>
<li>每次启动创建一个新的实例activity</li>
<li>谁启动了该activity实例，该activity就在其栈中</li>
<li>当我们用ApplicationContext去启动standard模式的Activity的时候会报错<br><br>  原因：standard模式的Activity默认会进入启动它的Activity所属的任务栈；applicationContext没有任务栈；<br><br/>解决： 启动Activity指定**intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)**标记位，singleTask模式</li>
</ol>
<h2 id="4-2栈顶复用模式-singleTop"><a href="#4-2栈顶复用模式-singleTop" class="headerlink" title="4.2栈顶复用模式-singleTop"></a>4.2栈顶复用模式-singleTop</h2><ol>
<li>如果目标activity实例位于栈顶，activity不会重新创建，同时onNewIntent()方法会被调用，不会调用onCreate()、onStart()</li>
<li>如果栈顶无目标实例，则重新创建activity</li>
</ol>
<h2 id="4-3栈内复用模式-singleTask【AndroidManifest的clearTop】"><a href="#4-3栈内复用模式-singleTask【AndroidManifest的clearTop】" class="headerlink" title="4.3栈内复用模式-singleTask【AndroidManifest的clearTop】"></a>4.3栈内复用模式-singleTask【AndroidManifest的clearTop】</h2><ol>
<li>可以理解为栈内单例模式的activity，只要栈内存在，就不会重新创建，且onNewIntent()方法会被调用，不会调用onCreate()、onStart()</li>
<li>启动时，会先查找activity的目标栈，不存在则创建栈，然后创建实例</li>
<li>如果目标栈存在，查找栈内是否存在目标activity，不存在则创建。</li>
<li>如果目标栈存在目标activity，且不在栈顶，则会将activity之上的实例出栈，将activity置栈顶</li>
</ol>
<h2 id="4-4单实例模式-singleInstance"><a href="#4-4单实例模式-singleInstance" class="headerlink" title="4.4单实例模式-singleInstance"></a>4.4单实例模式-singleInstance</h2><ol>
<li>加强版的singleTask模式，即activity独立运行在单独栈中</li>
</ol>
<h2 id="4-5-Intent的Flags"><a href="#4-5-Intent的Flags" class="headerlink" title="4.5 Intent的Flags"></a>4.5 Intent的Flags</h2><ol>
<li>FLAG_ACTIVITY_NEW_TASK:为activity指定【singleTask】启动模式</li>
<li>FLAG_ACTIVITY_SINGLE_TOP：指定【singleTop】</li>
<li>FLAG_ACTIVITY_CLEAR_TOP:与FLAG_ACTIVITY_NEW_TASK配合使用，效果同【singleTask】</li>
<li>FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:Activity不会出现在历史Activity的列表中,一次性activity</li>
</ol>
<h1 id="5-Activity数据传递"><a href="#5-Activity数据传递" class="headerlink" title="5.Activity数据传递"></a>5.Activity数据传递</h1><h2 id="5-1携带数据"><a href="#5-1携带数据" class="headerlink" title="5.1携带数据"></a>5.1携带数据</h2><p>step1:Activity1发送数据</p>
<pre class="highlight"><span class="line">String data = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">Intent intent = <span class="keyword">new</span> Intent(MainActivity.<span class="keyword">this</span>,FirstActivity.class);</span><br><span class="line">intent.putExtra(<span class="string">&quot;mydata&quot;</span>,data);</span><br><span class="line">startActivity(intent);</span><br></pre>
<p>step2:Activity2接受数据</p>
<pre class="highlight"><span class="line">Intent intent = getIntent();</span><br><span class="line">String data = intent.getStringExtra(<span class="string">&quot;mydata&quot;</span>);</span><br><span class="line">Toast.makeText(<span class="keyword">this</span>, <span class="string">&quot;传递过来的值是&quot;</span> + data, Toast.LENGTH_SHORT).show();</span><br></pre>
<h2 id="5-2返回数据"><a href="#5-2返回数据" class="headerlink" title="5.2返回数据"></a>5.2返回数据</h2><p>step1:activity1启动方式：要求返回数据</p>
<pre class="highlight"><span class="line">Intent intent = <span class="keyword">new</span> Intent(MainActivity.<span class="keyword">this</span>, FirstActivity.class);</span><br><span class="line">startActivityForResult(intent, <span class="number">1</span>);</span><br></pre>
<p>step2:activity2设置返回数据</p>
<pre class="highlight"><span class="line">Intent intent = <span class="keyword">new</span> Intent();</span><br><span class="line">intent.putExtra(<span class="string">&quot;data_return&quot;</span>,<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">setResult(RESULT_OK,intent);</span><br><span class="line">finish();</span><br></pre>
<p>step3:activity1接收返回的数据</p>
<pre class="highlight"><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onActivityResult</span><span class="params">(<span class="keyword">int</span> requestCode, <span class="keyword">int</span> resultCode, Intent data)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span> (requestCode)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">if</span> (resultCode == RESULT_OK)&#123;</span><br><span class="line">                String returnedData = data.getStringExtra(<span class="string">&quot;data_return&quot;</span>);</span><br><span class="line">                Log.e(<span class="string">&quot;MainActivity&quot;</span>,returnedData);</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>
<h1 id="6-activity全局管理"><a href="#6-activity全局管理" class="headerlink" title="6.activity全局管理"></a>6.activity全局管理</h1><p><strong>仅提供思路，实际开发按需更换为软引用方式</strong></p>
<p>step1:Activity管理类</p>
<pre class="highlight"><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ActivityCollector</span></span>&#123;</span><br><span class="line">     <span class="keyword">public</span> <span class="keyword">static</span> List&lt;Activity&gt; activities = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    </span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">addActivity</span><span class="params">( Activity activity )</span></span>&#123;</span><br><span class="line">          activities.add( activity );</span><br><span class="line">     &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">removeActivity</span><span class="params">( Activity activity )</span></span>&#123;</span><br><span class="line">           activities.remove( activity );</span><br><span class="line">     &#125; </span><br><span class="line"></span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">finishAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">            <span class="keyword">for</span>( Activity activity : activities)&#123;</span><br><span class="line">                  activity.finish();</span><br><span class="line">            &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre>
<p>step2:BaseActivity</p>
<pre class="highlight"><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BaseActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span></span>&#123;</span><br><span class="line">     <span class="meta">@Override</span></span><br><span class="line">     <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">( Bundle savedInstanceState )</span></span>&#123;</span><br><span class="line">          <span class="keyword">super</span>.onCreate( savedInstanceState );</span><br><span class="line">          Log.d( <span class="string">&quot;BaseActivity&quot;</span> , getClass().getSimpleName() );</span><br><span class="line">          ActivityCollector.addActivity(<span class="keyword">this</span>);</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onDestory</span><span class="params">()</span></span>&#123;</span><br><span class="line">           <span class="keyword">super</span>.onDestory();</span><br><span class="line">           ActivityCollector.removeActivity(<span class="keyword">this</span>);</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre>
<p>step3:XxxActivity</p>
<pre class="highlight"><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">XxxActivity</span> <span class="keyword">extends</span> <span class="title">BaseActivity</span></span>&#123;</span><br><span class="line">    <span class="comment">//....</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">exitApp</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ActivityCollector.finishAll();</span><br><span class="line">        <span class="comment">//or force exit</span></span><br><span class="line">        <span class="comment">//android.os.Process.killProcess(android.os.Process.myPid());</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre>


  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2020/11/03/035-Acticity%E8%AF%A6%E8%A7%A3-%E5%9F%BA%E7%A1%80&amp;%E8%BF%9B%E9%98%B6/">http://example.com/2020/11/03/035-Acticity%E8%AF%A6%E8%A7%A3-%E5%9F%BA%E7%A1%80&amp;%E8%BF%9B%E9%98%B6/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2020/11/11/Mac OS 软件清单/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">Mac OS 软件清单 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2020/11/01/Mac配置Java开发环境/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">Mac配置Java开发环境 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%A6%81"><span class="toc-text">1.概要</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-Activity%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.Activity生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1%E5%85%B8%E5%9E%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.1典型生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2%E5%BC%82%E5%B8%B8%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.2异常生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-configChanges"><span class="toc-text">2.3 configChanges</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Activity%E7%9A%84%E5%90%AF%E5%8A%A8%E8%BF%87%E7%A8%8B"><span class="toc-text">3.Activity的启动过程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Activity-%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F"><span class="toc-text">4.Activity 启动模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E6%A0%87%E5%87%86%E6%A8%A1%E5%BC%8F-standard"><span class="toc-text">4.1标准模式-standard</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%A0%88%E9%A1%B6%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTop"><span class="toc-text">4.2栈顶复用模式-singleTop</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E6%A0%88%E5%86%85%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTask%E3%80%90AndroidManifest%E7%9A%84clearTop%E3%80%91"><span class="toc-text">4.3栈内复用模式-singleTask【AndroidManifest的clearTop】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E5%8D%95%E5%AE%9E%E4%BE%8B%E6%A8%A1%E5%BC%8F-singleInstance"><span class="toc-text">4.4单实例模式-singleInstance</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-Intent%E7%9A%84Flags"><span class="toc-text">4.5 Intent的Flags</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Activity%E6%95%B0%E6%8D%AE%E4%BC%A0%E9%80%92"><span class="toc-text">5.Activity数据传递</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%90%BA%E5%B8%A6%E6%95%B0%E6%8D%AE"><span class="toc-text">5.1携带数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2返回数据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-activity%E5%85%A8%E5%B1%80%E7%AE%A1%E7%90%86"><span class="toc-text">6.activity全局管理</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%A6%81"><span class="toc-text">1.概要</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-Activity%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.Activity生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1%E5%85%B8%E5%9E%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.1典型生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2%E5%BC%82%E5%B8%B8%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.2异常生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-configChanges"><span class="toc-text">2.3 configChanges</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Activity%E7%9A%84%E5%90%AF%E5%8A%A8%E8%BF%87%E7%A8%8B"><span class="toc-text">3.Activity的启动过程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Activity-%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F"><span class="toc-text">4.Activity 启动模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E6%A0%87%E5%87%86%E6%A8%A1%E5%BC%8F-standard"><span class="toc-text">4.1标准模式-standard</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%A0%88%E9%A1%B6%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTop"><span class="toc-text">4.2栈顶复用模式-singleTop</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E6%A0%88%E5%86%85%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTask%E3%80%90AndroidManifest%E7%9A%84clearTop%E3%80%91"><span class="toc-text">4.3栈内复用模式-singleTask【AndroidManifest的clearTop】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E5%8D%95%E5%AE%9E%E4%BE%8B%E6%A8%A1%E5%BC%8F-singleInstance"><span class="toc-text">4.4单实例模式-singleInstance</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-Intent%E7%9A%84Flags"><span class="toc-text">4.5 Intent的Flags</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Activity%E6%95%B0%E6%8D%AE%E4%BC%A0%E9%80%92"><span class="toc-text">5.Activity数据传递</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%90%BA%E5%B8%A6%E6%95%B0%E6%8D%AE"><span class="toc-text">5.1携带数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2返回数据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-activity%E5%85%A8%E5%B1%80%E7%AE%A1%E7%90%86"><span class="toc-text">6.activity全局管理</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%A6%81"><span class="toc-text">1.概要</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-Activity%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.Activity生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1%E5%85%B8%E5%9E%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.1典型生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2%E5%BC%82%E5%B8%B8%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">2.2异常生命周期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-configChanges"><span class="toc-text">2.3 configChanges</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Activity%E7%9A%84%E5%90%AF%E5%8A%A8%E8%BF%87%E7%A8%8B"><span class="toc-text">3.Activity的启动过程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Activity-%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F"><span class="toc-text">4.Activity 启动模式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1%E6%A0%87%E5%87%86%E6%A8%A1%E5%BC%8F-standard"><span class="toc-text">4.1标准模式-standard</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2%E6%A0%88%E9%A1%B6%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTop"><span class="toc-text">4.2栈顶复用模式-singleTop</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3%E6%A0%88%E5%86%85%E5%A4%8D%E7%94%A8%E6%A8%A1%E5%BC%8F-singleTask%E3%80%90AndroidManifest%E7%9A%84clearTop%E3%80%91"><span class="toc-text">4.3栈内复用模式-singleTask【AndroidManifest的clearTop】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4%E5%8D%95%E5%AE%9E%E4%BE%8B%E6%A8%A1%E5%BC%8F-singleInstance"><span class="toc-text">4.4单实例模式-singleInstance</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-Intent%E7%9A%84Flags"><span class="toc-text">4.5 Intent的Flags</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Activity%E6%95%B0%E6%8D%AE%E4%BC%A0%E9%80%92"><span class="toc-text">5.Activity数据传递</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E6%90%BA%E5%B8%A6%E6%95%B0%E6%8D%AE"><span class="toc-text">5.1携带数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2返回数据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-activity%E5%85%A8%E5%B1%80%E7%AE%A1%E7%90%86"><span class="toc-text">6.activity全局管理</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>